//
//  ViewController.swift
//  MacSSaver
//
//  Created by lete on 2020/11/25.
//

import UIKit
import Foundation
import Cocoa

class ViewController: UIViewController {

    var saverPath:String = ""
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        //1.
        guard let path = Bundle.main.path(forResource: "MySaver",ofType:"saver") else {
          print("Unable to locate BuildScript.command")
          
          return
        }
        saverPath = path
        print("====>>>>> %@",path)
        
        //2.
//        self.buildTask = Process()
//        self.buildTask.launchPath = path
//        self.buildTask.arguments = arguments

    }

    @IBAction func btnHandle(_ sender: UIButton) {
        
        debugPrint("=====>>>>>>")
        
    
        let app = Dynamic.NSApplication.sharedApplication()

        debugPrint(saverPath)
        open(saverPath, 0)

        
        shell(["open", saverPath])
//        print(shell(["ls", "-al"]))

        
    }
    
}


@discardableResult
func shell(_ args: [String]) -> String {
    let task = Dynamic.NSTask()
    task.launchPath = "/usr/bin/env"
    task.arguments = args

    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    task.waitUntilExit()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String

    return output;
}
